package com.heavenbird.book.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.heavenbird.book.BookConst;
import com.heavenbird.book.dao.interfaces.IInsUserDAO;
import com.heavenbird.book.entity.InsUser;
import com.heavenbird.common.util.DBUtil;

public class InsUserDAOImpl implements IInsUserDAO {
	private static transient Log log = LogFactory.getLog(InsUserDAOImpl.class);
	
	public InsUser[] queryInsUser(String email) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(InsUser.class);
		criteria.add(Restrictions.eq("email", email));
		criteria.addOrder(Order.desc("createDate"));
		List list = criteria.list();
		return (InsUser[]) list.toArray(new InsUser[list.size()]);
	}

	public void updateTryTime(long userId, int tryTime) throws Exception {
		Session session = DBUtil.getSession();
		String hql = "update ins_user set try_time = ? where id = ?";
		Query query = session.createSQLQuery(hql);
		query.setInteger(0, tryTime);
		query.setLong(1, userId);
		int result = query.executeUpdate();
	}
	
	public void lockInsUser(long userId) throws Exception {
		Session session = DBUtil.getSession();
		String hql = "update ins_user set lock_status = ?, lock_date = ? where id = ?";
		Query query = session.createSQLQuery(hql);
		query.setInteger(0, BookConst.INSUSER.LOCK_STATUS_1);
		query.setTimestamp(1, DBUtil.getSysDate());
		query.setLong(2, userId);
		int result = query.executeUpdate();
	}
	
	public void newInsUser(InsUser insUser) throws Exception {
		Session session = DBUtil.getSession();
		session.save(insUser);
	}
}
